package ModelLayer;
import java.util.ArrayList;
/**
 * Write a description of class Order here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Order
{
    private String id;
    private boolean with_delivery;
    private String delivery_note;
    private String additional_information;
    private String terms_of_delivery;
    private boolean is_complete;
    private ArrayList<SalesLineItem> items;
    private double total_price;
    private double special_discount;

 
    public Order(String i_id, boolean i_with_delivery, String i_delivery_note, String i_additional_information, String  i_terms_of_delivery, ArrayList<String> ids, ArrayList<String> amounts, boolean i_is_complete, double i_total_price, double i_special_discount)
    {
        items = new ArrayList<>();
        id = i_id;
        with_delivery = i_with_delivery;
        delivery_note = i_delivery_note;
        additional_information =  i_additional_information;
        terms_of_delivery = i_terms_of_delivery;
        is_complete = i_is_complete;
        total_price = i_total_price;
        fill_in(ids, amounts);
        special_discount = i_special_discount;
    }
    
    /*******************************************GETTERS****************************************/
    
    public String get_id()
    {
        return id;
    }
    
    public boolean get_with_delivery()
    {
        return with_delivery;
    }
    
    public String get_additional_information()
    {
        return additional_information;
    }
    
    public String get_terms_of_delivery()
    {
        return terms_of_delivery;
    }
    
    public ArrayList<String> get_art_nrs()
    {
        ArrayList<String> art_nrs = new ArrayList<>();
        for(SalesLineItem s : items)
        {
            art_nrs.add(s.get_id());
        }
        return art_nrs;
    }
    
    public double get_total_price()
    {
        return total_price;
    }
    
    /**************************************SETTERS******************************************/
    
    public void set_id(String i_id)
    {
        id = i_id;
    }
    
    public void set_with_delivery(boolean i_with_delivery)
    {
        with_delivery = i_with_delivery;
    }
    
    public void set_additional_information(String i_additional_information)
    {
        additional_information = i_additional_information;
    }
    
    public boolean get_is_complete()
    {
        return is_complete;
    }
    
    public void set_is_complete(boolean i_is_complete)
    {
        is_complete = i_is_complete;
    }
    
    public Object[] get_fields()
    {
    	Object[] fields = { with_delivery, delivery_note, additional_information, terms_of_delivery, special_discount, is_complete };
    	return fields;
    }
    
    public String give_data_string()
    {
    	String temp_delivery_note = delivery_note.replaceAll("\t", "//INSERT TABULATION//");
    	String temp_terms_of_delivery = terms_of_delivery.replaceAll("\t", "//INSERT TABULATION//");
    	String temp_additional_information = additional_information.replaceAll("\t", "//INSERT TABULATION//");
        return String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",id, with_delivery, temp_delivery_note, temp_additional_information, temp_terms_of_delivery, get_ids(), get_amounts(), is_complete, total_price, special_discount);
    }
    
    private String get_ids()
    {
        String ids = "";
        boolean first = true;
        for(SalesLineItem s : items)
        {
            if(first)
            {
                ids += s.get_id();
                first = false;
            }
            else
            {
                ids+= " " + s.get_id();
            }
        }
        return ids;
    }
    
    private String get_amounts()
    {
        String amounts = "";
        boolean first = true;
        for(SalesLineItem s : items)
        {
            if(first)
            {
                amounts += s.get_amount();
                first = false;
            }
            else
            {
                amounts += " " +  s.get_amount();
            }
        }
        return amounts;
    }
    
    public ArrayList<String> provide_ids()
    {
    	ArrayList<String> ids = new ArrayList<>();
    	for(SalesLineItem s : items)
        {
    		ids.add(s.get_id());
        }
    	return ids;
    }
    
    public ArrayList<String> provide_amounts()
    {
    	ArrayList<String> amounts = new ArrayList<>();
    	for(SalesLineItem s : items)
        {
    		amounts.add(String.valueOf(s.get_amount()));
        }
    	return amounts;
    }


    private void fill_in(ArrayList<String> ids,ArrayList<String> amounts)
    {
        if(ids.contains("") == false)
        {
            for(int i = 0; i < ids.size(); i++)
            {
                items.add(new SalesLineItem(Stock.find_product_by_art_nr(ids.get(i)), Integer.parseInt(amounts.get(i))));
            }
        }
    }
    
    private String convert_is_complete()
    {
        if (is_complete)
        {
            return "Closed";
        }
        return "Open";
    }
    
    private String convert_with_delivery()
    {
        if(with_delivery)
        {
            return "Yes";
        }
        return "No";
    }
    
    public String printable()
    {
        if(!(with_delivery) || (terms_of_delivery.equals("0") && delivery_note.equals("0")))
        {
            return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nAdditional Information:\t\t%s \nState:\t\t\t\t%s \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), additional_information, convert_is_complete(), total_price ,ids_printable());
        }            
        else if (terms_of_delivery.equals("0"))
        {
            return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nDelivery note:\t\t\t%s \nAdditional Information:\t\t%s  \nState:\t\t\t\t%s \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), delivery_note, additional_information, convert_is_complete(), total_price ,ids_printable());
        }
        else if (delivery_note.equals("0"))
        {
            return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nTerms of delivery:\t\t%s \nAdditional Information:\t\t%s  \nState:\t\t\t\t%s \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), terms_of_delivery, additional_information, convert_is_complete(), total_price ,ids_printable());
        }
        return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nDelivery note:\t\t\t%s \nTerms of delivery:\t%s \nAdditional Information:\t\t%s  \nState:\t\t\t\t%s \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), delivery_note, terms_of_delivery, additional_information, convert_is_complete(), total_price ,ids_printable());
    }
    
    public String open_printable()
    {
        if(!(with_delivery) || (terms_of_delivery.equals("0") && delivery_note.equals("0")))
        {
            return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nAdditional Information:\t\t%s \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), additional_information, total_price ,ids_printable());
        }            
        else if (terms_of_delivery.equals("0"))
        {
            return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nDelivery note:\t\t\t%s \nAdditional Information:\t\t%s  \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), delivery_note, additional_information, total_price ,ids_printable());
        }
        else if (delivery_note.equals("0"))
        {
            return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nTerms of delivery:\t\t%s \nAdditional Information:\t\t%s  \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), terms_of_delivery, additional_information, total_price ,ids_printable());
        }
        return String.format("Order ID:\t\t\t%s \nWith Delivery:\t\t\t%s \nDelivery note:\t\t\t%s \nTerms of delivery:\t%s \nAdditional Information:\t\t%s  \nTotal price of the order\t%s \nProduct IDs:\t%s \n " ,id, convert_with_delivery(), delivery_note, terms_of_delivery, additional_information, total_price ,ids_printable());
    }
    

    
    private String ids_printable()
    {
        String ids = "";
        boolean first = true;
        for(SalesLineItem s : items)
        {
            if(first)
            {
                ids += "\t\t" + s.get_id() + "\n";
                first = false;
            }
            else
            {
                ids += "\t\t\t\t" + s.get_id() + "\n";
            }
        }
        return ids;
    }
    
    private SalesLineItem find_by_art_nr(String art_nr)
    {
        for (SalesLineItem s : items)
        {
            if (s.get_id().equals(art_nr))
            {
                return s;
            }
        }
        return null;
    }
    
    public void clear_product(String art_nr)
    {
        items.remove(find_by_art_nr(art_nr));
    }
    
    public void calculate_total_price(double group_discount, double maximum_discount, double pickup_discount)
    {
        double temp_total_price = 0;
        for (SalesLineItem s : items)
        {
            temp_total_price += s.get_total_cost();
        }
        
        if(special_discount != 0)
        {
            total_price = temp_total_price * (1 - special_discount / 100);
        }
        else
        {
            double price_after_discount = 0;
            for (SalesLineItem s : items)
            {
                price_after_discount += s.get_discounted_cost();
            }
            price_after_discount *= (1- group_discount / 100);
            if(!(with_delivery))
            {
                price_after_discount *= (1- pickup_discount / 100);
            }
            
            double minimum_total_price = temp_total_price* (1 - maximum_discount/100);
            if (price_after_discount > minimum_total_price)
            {
                total_price = price_after_discount;
            }
            else
            {
                total_price = minimum_total_price;
            }
        }
    }
    
 }

